﻿// <copyright file="LogViewer.xaml.cs" company="Snowman Consulting">
//     Copyright (c) 2009 Snowman Consulting. All rights reserved.
// </copyright>

namespace SnowmanConsulting.ProductKeyManager.UI
{
    using System;
    using System.IO;
    using System.Reflection;
    using System.Windows;
    using SnowmanConsulting.ProductKeyManager.Logging;

    /// <summary>
    /// Interaction logic for LogViewer.xaml
    /// </summary>
    public partial class LogViewer : Window
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LogViewer"/> class.
        /// </summary>
        public LogViewer()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(this.LogViewer_Loaded);
        }

        /// <summary>
        /// Handles the Loaded event of the LogViewer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void LogViewer_Loaded(object sender, RoutedEventArgs e)
        {
            Assembly assm = Assembly.GetExecutingAssembly();
            string logFileName = assm.Location.Replace("ProductKeyManager.exe", Logger.LogFile);
            try
            {
                FileStream log = File.Open(logFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                using (TextReader tr = new StreamReader(log))
                {
                    this.mLogText.Text = tr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// Handles the Click event of the MenuFileClose control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void MenuFileClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
